package com.mine.redis.util.service;

import com.mine.redis.util.mapper.FollowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * @author weixiao
 * @date 2024/8/19 10:24
 */
@Service
public class FollowService {

    @Autowired
    private FollowMapper followMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public void follow(Integer followId, Boolean isFollow, Integer userId) {
        if (isFollow) {
            // 关注
            int res = followMapper.insert(userId, followId);
            if (res > 0) {
                stringRedisTemplate.opsForSet().add("follow:" + userId, followId.toString());
            }
        } else {
            // 取关
            int res = followMapper.delete(userId, followId);
            if (res > 0) {
                stringRedisTemplate.opsForSet().remove("follow:" + userId, followId.toString());
            }
        }
    }

    public boolean isFollow(Integer followId, Integer userId) {
        return followMapper.isFollow(userId, followId) > 0;
    }

    public boolean common(Integer followId, Integer userId) {
        Set<String> commonSet = stringRedisTemplate.opsForSet().intersect("follow:" + followId, "follow:" + userId);
        assert commonSet != null;
        for (String s : commonSet) {
            System.out.println(s);
        }
        return true;
    }
}
